package com.ply.flashsalessystem.utils;

import com.ply.flashsalessystem.entity.pojo.OrderTime;
import com.ply.flashsalessystem.mapper.OrderTimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author XiaoPan
 * date: 2022/4/3 13:24
 * <p>
 * action:
 */
public class DateUtils {

    /**
     * 获取 上一次的订单时间
     * @param orderTime orderTime对象
     */
    public static Date getOrderTime1(OrderTime orderTime) {
        // 获取到时间  做比较
        LocalTime time = orderTime.getTime();
//        LocalDateTime localTime1 = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

        LocalDateTime now = LocalDateTime.now();
//        if (LocalTime.now().compareTo(localTime1.toLocalTime()) < 0) {
        if (LocalTime.now().compareTo(time) < 0) {
            // 天数减一
            now = now.minusDays(1);
        }

        now = now
                .withHour(time.getHour())
                .withMinute(time.getMinute())
                .withSecond(0).withNano(0);
        return Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
    }
    /**
     * 将 Date 转化 成 LocalDateTime
     */

    public static LocalDateTime DateTo(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 将 LocalDateTime 转化 成 Date
     */
    public static Date LocalDateTimeTo(LocalDateTime localDateTime){
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
